﻿// --------------------------------
// <copyright file="GameList.cs" company="Pattern Web Solutions">
//     Copyright © Simon Gill 2010. Distributed under the GPL.
// </copyright>
// <author>Simon Gill</author>
// ---------------------------------
// This file is part of League Manager

// League Manager is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.

// League Manager is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with League Manager.  If not, see <http://www.gnu.org/licenses/>.

namespace LeagueManager
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using LeagueManager.Models;

    public partial class GameList : Form
    {
        private IMembershipRepository membership;

        public GameList(IMembershipRepository repository)
        {
            this.InitializeComponent();

            this.membership = repository;

            this.LoadGames();
        }

        private void LoadGames()
        {
            this.lstGames.Items.Clear();
            this.lstGames.Items.AddRange(this.membership.AllGames().ToArray());

            if (this.lstGames.Items.Count > 1)
            {
                this.lstGames.SelectedIndex = 0;
            }
            else
            {
                this.lstGames.SelectedIndex = -1;
            }
        }

        private void AddGame_Click(object sender, EventArgs e)
        {
            Game newGame = new Game();

            if (!string.IsNullOrEmpty(this.txtNewGame.Text))
            {
                newGame.Name = this.txtNewGame.Text;
                newGame.Active = true;
                this.membership.AddGame(newGame);
            }

            this.txtNewGame.Text = string.Empty;
            this.LoadGames();
        }

        private void ToggleActive_Click(object sender, EventArgs e)
        {
            (this.lstGames.SelectedItem as Game).Active = !(this.lstGames.SelectedItem as Game).Active;
            this.membership.SaveChanges();
            this.LoadGames();
        }

        private void Games_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.lstGames.SelectedIndex < 0)
            {
                this.lstGames.SelectedIndex = 0;
            }

            this.btnToggleActive.Text = (this.lstGames.SelectedItem as Game).Active ? "Mark Game Inactive" : "Mark Game Active";
        }
    }
}
